let text = require('../../../../common/util/text.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    week:'',
    treatment_item_id:'',
    action_list:[],
    baseline:false,
    complete:false,
    isone:false,
    type:'',
    showtips:false,
    show:false,
    PracticeStatus:false,
    xPageReady:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let that =this;
      console.log(options)
      that.xToPracticeStatus()
      that.setData({
        week:Number(options.week),
        treatment_item_id:options.treatment_item_id,
        type:Number(options.type)
      })
  },

  /**
   *   监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.xInit(this.data.week,this.data.treatment_item_id)
  },
  xInit:function(week,id){
    let that =this;
    let _dataObj = {
      week: week,
      treatment_item_id: id
    }
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
        action: "get_action_list",
        dataObj: _dataObj
      }
    }).then(res => {
       that.setData({
        action_list:res.result,
        xPageReady:false
       })
       for(let index in that.data.action_list){
         that.setData({
            show:!that.data.action_list[0].baseline_reading_status,
            baseline:!that.data.action_list[0].baseline_reading_status
         })
         if(that.data.action_list[index].is_complete_today==false){
           that.setData({
            complete:false
          })
          break;
         }else{
          that.setData({
            complete:true
          })
         }
       }
    }).catch(err=>{
      that.setData({
        xPageReady:false
       })
    })
    
  },
  // 动作详情
  xToInfo:function(e){
      let that =this;
      let action_id = e.currentTarget.dataset.action_id
      let name =e.currentTarget.dataset.name
      wx.navigateTo({
        url: '/subtool/pages/action/details/index?week='+that.data.week+'&action_id='+ action_id+'&treatment_item_id='+that.data.treatment_item_id+'&type='+that.data.type+'&name='+name,
      })
  },
  // 查看基线说明
  xToseebaseline:function(){
      this.setData({
        show:true,
        baseline:true,
        showtips:false
      })
  },
  // xToTips:function(){
  //   wx.showModal({
  //     title: "提示",
  //     content: "此项任务今天已完成，请明天继续",
  //     showCancel: false,
  //     confirmText:'好的'
  //   })
  // },
  xTonext:function(){
      let that =this;
      let _dataObj = {
        behavior_number:"NOJX000001"//行为编号,对应行为编号表
      }
      wx.cloud.callFunction({
          name: 'behavior_record',
          data: {
              action: "add",
              dataObj: _dataObj
          }
      }).then(res => {
        that.setData({
          show:false,
          baseline:false
        })
      })
  },
  
xToPracticeStatus:function(){
  let that =this;
  wx.cloud.callFunction({
    name: 'practice_help',
    data: {
      action: "get_practice_help_status",
      dataObj: {
        num:4
      }
    }
  }).then(res => {
    for(let index in res.result){
      if(res.result[index]._id==1){
        that.setData({
          PracticeStatus:res.result[index].status
        })
      }
    }
  })
},
xTosetM:function(){
  let _dataObj = {
    num:4,//练习任务类型数值
    type:1,//1:练习说明
  }
  wx.setStorageSync(this.data.treatment_item_id, this.data.treatment_item_id)
  wx.cloud.callFunction({
    name: 'practice_help',
    data: {
      action: "get_practice_help",
      dataObj: _dataObj
    }
  }).then(res => {
    res.result[0].content=text.text(res.result[0].content)
    this.setData({
      show:!this.data.showtips,
      showtips:!this.data.showtips,
      practice_help:res.result[0],
      baseline:false
    })
  })
    
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('页面隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log('页面卸载')
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})